;; Book mark 1

(global-set-key [S-f1] 'bookmark-jump-default1)

(defun bookmark-jump-default1 (pos)
  "Set a default bookmark 1 default-bookmark1 at current position"
  (interactive "d")
  (bookmark-jump "default-bookmark1")
  (bookmark-set "default-bookmark1"))

(global-set-key [C-f1] 'bookmark-set-default1)
(defun bookmark-set-default1 (pos)
  "Jump to the default bookmark 1 default-bookmark1"/
  (interactive "d")
  (bookmark-set "default-bookmark1"))

(global-set-key (kbd "<C-f2>") 'bm-toggle)
(global-set-key (kbd "<f2>")   'bm-next)
(global-set-key (kbd "<S-f2>") 'bm-previous)

(global-set-key [(control f4)] 'highlight-symbol-at-point)
(global-set-key [f4] 'highlight-symbol-next)
(global-set-key [(shift f4)] 'highlight-symbol-prev)
(global-set-key [(meta f4)] 'highlight-symbol-prev)

;; Set the word search keys
(define-key global-map [f3] 'isearch-forward)
(define-key isearch-mode-map [f3] 'isearch-repeat-forward)

(define-key global-map [(control f)] 'isearch-forward)
(define-key isearch-mode-map [(control f)] 'isearch-repeat-forward)

(define-key global-map [C-f3] 'isearch-forward-regexp)
(define-key global-map [S-f3] 'isearch-backward)
(define-key isearch-mode-map [S-f3] 'isearch-repeat-backward)

(define-key global-map [C-r] 'isearch-backward)
(define-key isearch-mode-map [C-r] 'isearch-repeat-backward)

(define-key global-map [C-S-f3] 'isearch-backward-regexp)


;; Go to line
(global-set-key [f5] 'goto-line)

(global-set-key [C-f6] 'rename-buffer)
;; Search previour/next issued command
(global-set-key [S-f7] 'comint-next-matching-input-from-input)
(global-set-key [f7] 'comint-previous-matching-input-from-input)

;; Replace
(global-set-key [f9] 'query-replace)
(global-set-key [C-f9] 'query-replace-regexp)
(global-set-key [S-f9] 'query-replace-reg-t)

(defun query-replace-reg-t (to-string)
  (interactive (let (to)
		 (setq to (read-from-minibuffer
			   (format "Query-replace \"%s\" with: "
				   (get-register t))
			   nil nil nil
			   query-replace-to-history-variable nil t))
		 (list to)))
  (perform-replace (get-register t) to-string t nil nil))

(global-set-key [f10] 'replace-string)
(global-set-key [C-f10] 'replace-string-regexp)
(global-set-key [S-f10] 'replace-string-reg-t)

(defun replace-string-reg-t (to-string)
  (interactive (let (to)
		 (setq to (read-from-minibuffer
			   (format "Replace \"%s\" with: "
				   (get-register t))
			   nil nil nil
			   query-replace-to-history-variable nil t))
		 (list to)))
(perform-replace (get-register t) to-string nil nil nil))

(defun switch-source-file ()
  (interactive)
  (let
  (setq file-name (buffer-file-name))
  (if (string-match "\\.cpp" file-name)
      (find-file (replace-regexp-in-string "\\.cpp" "\.h" file-name)))
  (if (string-match "\\.h" file-name)
      (find-file (replace-regexp-in-string "\\.h" "\.cpp" file-name)))
  )
)

(defun switch-source-buffer ()
  (interactive)
  (let
  (setq file-name (buffer-file-name))
  (if (string-match "\\.cpp" file-name)
      (switch-to-buffer (replace-regexp-in-string "\\.cpp" "\.h" (file-name-nondirectory file-name))))
  (if (string-match "\\.h" file-name)
      (switch-to-buffer (replace-regexp-in-string "\\.h" "\.cpp"  (file-name-nondirectory file-name)))))
)
(global-set-key [C-f11] 'switch-source-file)
(global-set-key [M-f11] 'switch-source-buffer)

(defun flush-source-buffer ()
  (interactive)
  (setq file-name (buffer-file-name))
  (find-file file-name)
)

(global-set-key [f8] 'flush-source-buffer)
(global-set-key "\C-o" 'find-file)
(global-set-key "\C-s" 'save-buffer)
(global-set-key "\C-p" 'pwd)
(global-set-key [C-backspace] 'backward-kill-word)
(global-set-key [C-delete] 'kill-word)
(global-set-key "\C-d" 'kill-whole-line)

(defun kill-whole-line ()
  "Kill the whole line the cursor located"
  (interactive)
  (beginning-of-line nil)
  (kill-line nil)
  (kill-line nil))

(defun match-paren (arg)
  "Go to the matching paren if on a paren; otherwise insert %."
  (interactive "p")
  (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
		((looking-at "\\s\)") (forward-char 1) (backward-list 1))
		(t (self-insert-command (or arg 1)))))

(global-set-key (kbd "C-%") 'match-paren)
(global-set-key (kbd "C-{") 'beginning-of-buffer)
(global-set-key (kbd "C-}") 'end-of-buffer)


(global-set-key [M-left] 'windmove-left)
(global-set-key [M-right] 'windmove-right)
(global-set-key [M-up] 'windmove-up)
(global-set-key [M-down] 'windmove-down)


(global-set-key [(f6)] 'ibuffer)

(global-unset-key (kbd "C-x C-c"))

(global-unset-key (kbd "C-SPC"))

(global-set-key (kbd "M-'") 'imenu-list-smart-toggle)
